/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file    usart.c
  * @brief   This file provides code for the configuration
  *          of the USART instances.
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "usart.h"
#include "string.h"
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

UART_HandleTypeDef huart1;

/* USART1 init function */

#define USART_MAX_PORTS 2 // 可根据实际需求扩展

typedef struct {
    UART_HandleTypeDef huart;
    DMA_HandleTypeDef hdma_tx;
    DMA_HandleTypeDef hdma_rx;
    uint8_t rx_buffer[256];
    uint8_t tx_buffer[256];
} USART_Port_t;

static USART_Port_t usart_ports[USART_MAX_PORTS];

void USART_Port_Init(uint8_t port, USART_TypeDef *instance, uint32_t baudrate)
{
    USART_Port_t *p = &usart_ports[port];
    p->huart.Instance = instance;
    p->huart.Init.BaudRate = baudrate;
    p->huart.Init.WordLength = UART_WORDLENGTH_8B;
    p->huart.Init.StopBits = UART_STOPBITS_1;
    p->huart.Init.Parity = UART_PARITY_NONE;
    p->huart.Init.Mode = UART_MODE_TX_RX;
    p->huart.Init.HwFlowCtl = UART_HWCONTROL_NONE;
    p->huart.Init.OverSampling = UART_OVERSAMPLING_16;

    HAL_UART_Init(&p->huart);

    // DMA 初始化（这里只做了简单示例，实际需根据芯片和端口调整）
    // TX DMA
    p->hdma_tx.Instance = DMA1_Channel4;
    p->hdma_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
    p->hdma_tx.Init.PeriphInc = DMA_PINC_DISABLE;
    p->hdma_tx.Init.MemInc = DMA_MINC_ENABLE;
    p->hdma_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
    p->hdma_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
    p->hdma_tx.Init.Mode = DMA_NORMAL;
    p->hdma_tx.Init.Priority = DMA_PRIORITY_LOW;
    HAL_DMA_Init(&p->hdma_tx);
    __HAL_LINKDMA(&p->huart, hdmatx, p->hdma_tx);

    // RX DMA
    p->hdma_rx.Instance = DMA1_Channel5;
    p->hdma_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
    p->hdma_rx.Init.PeriphInc = DMA_PINC_DISABLE;
    p->hdma_rx.Init.MemInc = DMA_MINC_ENABLE;
    p->hdma_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
    p->hdma_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
    p->hdma_rx.Init.Mode = DMA_NORMAL;
    p->hdma_rx.Init.Priority = DMA_PRIORITY_HIGH;
    HAL_DMA_Init(&p->hdma_rx);
    __HAL_LINKDMA(&p->huart, hdmarx, p->hdma_rx);

    // 启动DMA接收
    HAL_UART_Receive_DMA(&p->huart, p->rx_buffer, sizeof(p->rx_buffer));
}

int USART_Port_Send(uint8_t port, uint8_t *data, uint16_t len)
{
    USART_Port_t *p = &usart_ports[port];
    if (len > sizeof(p->tx_buffer)) return -1;
    memcpy(p->tx_buffer, data, len);
    if (HAL_UART_Transmit_DMA(&p->huart, p->tx_buffer, len) == HAL_OK)
        return 0;
    return -2;
}

int USART_Port_Receive(uint8_t port, uint8_t *data, uint16_t len)
{
    USART_Port_t *p = &usart_ports[port];
    if (len > sizeof(p->rx_buffer)) return -1;
    memcpy(data, p->rx_buffer, len);
    return 0;
}

// 需要在 HAL_UART_RxCpltCallback 中处理接收完成事件
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
    // 用户可在此处理接收到的数据
    // 例如通知应用层
}


void MX_USART1_UART_Init(void)
{

  /* USER CODE BEGIN USART1_Init 0 */

  /* USER CODE END USART1_Init 0 */

  /* USER CODE BEGIN USART1_Init 1 */

  /* USER CODE END USART1_Init 1 */
  huart1.Instance = USART1;
  huart1.Init.BaudRate = 115200;
  huart1.Init.WordLength = UART_WORDLENGTH_8B;
  huart1.Init.StopBits = UART_STOPBITS_1;
  huart1.Init.Parity = UART_PARITY_NONE;
  huart1.Init.Mode = UART_MODE_TX_RX;
  huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  if (HAL_UART_Init(&huart1) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN USART1_Init 2 */

  /* USER CODE END USART1_Init 2 */

}

void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
{

  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(uartHandle->Instance==USART1)
  {
  /* USER CODE BEGIN USART1_MspInit 0 */

  /* USER CODE END USART1_MspInit 0 */
    /* USART1 clock enable */
    __HAL_RCC_USART1_CLK_ENABLE();

    __HAL_RCC_GPIOA_CLK_ENABLE();
    /**USART1 GPIO Configuration
    PA9     ------> USART1_TX
    PA10     ------> USART1_RX
    */
    GPIO_InitStruct.Pin = GPIO_PIN_9;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    GPIO_InitStruct.Pin = GPIO_PIN_10;
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    /* USART1 interrupt Init */
    HAL_NVIC_SetPriority(USART1_IRQn, 6, 0);
    HAL_NVIC_EnableIRQ(USART1_IRQn);
  /* USER CODE BEGIN USART1_MspInit 1 */

  /* USER CODE END USART1_MspInit 1 */
  }
}

void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
{

  if(uartHandle->Instance==USART1)
  {
  /* USER CODE BEGIN USART1_MspDeInit 0 */

  /* USER CODE END USART1_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_USART1_CLK_DISABLE();

    /**USART1 GPIO Configuration
    PA9     ------> USART1_TX
    PA10     ------> USART1_RX
    */
    HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_10);

    /* USART1 interrupt Deinit */
    HAL_NVIC_DisableIRQ(USART1_IRQn);
  /* USER CODE BEGIN USART1_MspDeInit 1 */

  /* USER CODE END USART1_MspDeInit 1 */
  }
}

/* USER CODE BEGIN 1 */

/* USER CODE END 1 */
